﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CN.DBML;

namespace CityNavigation
{
    public partial class LineView : System.Web.UI.Page
    {
        private CityNavigationDataContext dc = new CityNavigationDataContext(System.Configuration.ConfigurationManager.ConnectionStrings["CityNavigationConnectionString"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (this.Page.Request.QueryString["id"] != null)
                {
                    this.lblLineName.Text = this.Page.Request.QueryString["id"].ToString();
                    string lines = this.getLinePSByName(this.lblLineName.Text);
                    this.lblLinePS1.Text = lines.Split(',')[0];
                    this.lblLinePS2.Text = lines.Split(',')[1];
                }
            }
        }

        /// <summary>
        /// 根据线路名称取得线路所有站
        /// </summary>
        /// <param name="lineName">线路名称</param>
        /// <returns>所有站的字符串,包含上行下行</returns>
        private string getLinePSByName(string lineName)
        {
            System.Text.StringBuilder lineSb = new System.Text.StringBuilder();
            var linesList = from item in dc.BusPassStations
                            where item.LineName == lineName
                            orderby item.SerialNum
                            select item;
            //上行
            foreach (var item in linesList.Where(item => item.DirectionType == 1).ToList())
            {
                lineSb.Append(item.StationName);
                lineSb.Append('-');
            }
            lineSb.Remove(lineSb.Length - 1, 1);
            lineSb.Append(',');
            //下行
            foreach (var item in linesList.Where(item => item.DirectionType == 2).ToList())
            {
                lineSb.Append(item.StationName);
                lineSb.Append('-');
            }
            return lineSb.ToString().TrimEnd('-');
        }
    }
}